//
//  LGLungFollowAuditCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/8/8.
//

#import "LGLungFollowAuditCell.h"

@interface LGLungFollowAuditCell()

/**   */
@property (nonatomic , strong)UIImageView *headPic;
/**   */
@property (nonatomic , strong)UILabel *nameLabel;
/**   */
@property (nonatomic , strong)UILabel *ageLabel;
/**   */
@property (nonatomic , strong)UILabel *operateLabel;
/**   */
@property (nonatomic , strong)UILabel *auditLabel;
/**   */
@property (nonatomic , strong)UIView *itemView;
/**   */
@property (nonatomic , strong)NSMutableArray *tipArray;

@end

@implementation LGLungFollowAuditCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [self.headPic sd_setImageWithURL:[NSURL URLWithString:LGNSString(dataDic[@"avatar_url"])]];
    self.nameLabel.text = LGNSString(dataDic[@"realname"]);
    //性别、年龄
    NSString *gender = LGNSString(dataDic[@"gender"]);
    NSString *age = LGNSString(dataDic[@"age"]);
    if(gender.length>0 && age.length >0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@|%@岁",gender,age];
    }else if (gender.length > 0 && age.length == 0){
        self.ageLabel.text = gender;
    }else if (gender.length == 0 && age.length>0){
        self.ageLabel.text = [NSString stringWithFormat:@"%@岁",age];
    }else{
        self.ageLabel.text = @"";
    }
    
    //最近手术
    NSString *operateDate = LGNSString(dataDic[@"surgery_date"]);
    CGFloat operateTop = [operateDate isEmptyString]?0:viewPix(6);
    self.operateLabel.text = [operateDate isEmptyString]?@"":[NSString stringWithFormat:@"最近手术:%@",operateDate];
    [self.operateLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(operateTop);
    }];
    
    //tips
    NSArray *itemArray = dataDic[@"tips"];
    for (UILabel *item in self.tipArray) {
        [item removeFromSuperview];
    }
    [self.tipArray removeAllObjects];
    CGFloat itemTop = 0;
    CGFloat itemLeft = 0;
    for (NSInteger i=0; i<itemArray.count; i++) {
        CGFloat itemWidth = [itemArray[i] textWidthWithFont:LGFont(10) maxHeight:MAXFLOAT];
        itemTop = ((itemLeft+itemWidth+viewPix(16))>=Screen_W-viewPix(211)) ? itemTop+viewPix(22):itemTop;
        itemLeft = ((itemLeft+itemWidth+viewPix(16))>=Screen_W-viewPix(211)) ? 0:itemLeft;
        UILabel *itemLabel = [UILabel lableWithText:itemArray[i] colorString:@"#5E8EF5" textFont:LGFont(10) textAlignment:NSTextAlignmentCenter lines:1];
        itemLabel.frame = CGRectMake(itemLeft, itemTop, itemWidth+viewPix(14), viewPix(14));
        itemLabel.backgroundColor = [UIColor colorWithString:@"#E6EEFF"];
        itemLabel.cornerRidus = viewPix(7);
        [self.itemView addSubview:itemLabel];
        [self.tipArray addObject:itemLabel];
        itemLeft += itemWidth+viewPix(20);
    }
    CGFloat minHeight = (itemArray.count == 0 && [operateDate isEmptyString])?viewPix(20):0;
    CGFloat itemHeight = (itemArray.count>0) ? itemTop+viewPix(20) : minHeight;
    [self.itemView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(itemHeight));
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.headPic];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.ageLabel];
    [self.contentView addSubview:self.operateLabel];
    [self.contentView addSubview:self.auditLabel];
    [self.contentView addSubview:self.itemView];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headPic);
        make.left.equalTo(self.headPic.mas_right).offset(viewPix(8));
    }];
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(viewPix(6));
        make.centerY.equalTo(self.nameLabel);
        make.right.lessThanOrEqualTo(self.auditLabel.mas_left).offset(-viewPix(5));
    }];
    [self.auditLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.centerY.equalTo(self.headPic);
        make.width.equalTo(@(viewPix(120)));
        make.height.equalTo(@(viewPix(32)));
    }];
    [self.operateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(viewPix(6));
        make.left.equalTo(self.nameLabel);
    }];
    [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.operateLabel.mas_bottom).offset(viewPix(6));
        make.left.equalTo(self.nameLabel);
        make.right.equalTo(self.auditLabel.mas_left).offset(-viewPix(5));
        make.bottom.equalTo(self.contentView).offset(-viewPix(10));
        make.height.equalTo(@(viewPix(20)));
    }];
}
-(UIImageView *)headPic{
    if(!_headPic){
        _headPic = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(10), viewPix(46), viewPix(46))];
        _headPic.cornerRidus = 8;
    }
    return _headPic;
}

-(UILabel *)nameLabel{
    if(!_nameLabel){
        _nameLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _nameLabel;
}

-(UILabel *)ageLabel{
    if(!_ageLabel){
        _ageLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _ageLabel;
}

-(UILabel *)operateLabel{
    if(!_operateLabel){
        _operateLabel = [UILabel lableWithText:@"" colorString:@"#999999" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _operateLabel;
}

-(UILabel *)auditLabel{
    if(!_auditLabel){
        _auditLabel = [UILabel lableWithText:@"审批随访方案" colorString:@"#5E8EF5" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        [_auditLabel addBorder:@"#5E8EF5" lineWidth:1 cornerRidus:viewPix(16)];
    }
    return _auditLabel;
}

-(UIView *)itemView{
    if(!_itemView){
        _itemView = [[UIView alloc]init];
    }
    return _itemView;
}

-(NSMutableArray *)tipArray{
    if(!_tipArray){
        _tipArray = [NSMutableArray array];
    }
    return _tipArray;
}

@end
